﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AISFMIOperator
{
     partial class AddInsurance : Form
    {
        private Insurance insurance;
        private Patient patient;

        public AddInsurance(Patient patient)
        {
            InitializeComponent();
          
            this.patient = patient;
            this.patient.Insurances = new List<Insurance>();
            this.insurance = new Insurance();
            this.insurance.InsuranceID = 0;
            this.insurance.InsurancePrograms = new List<InsuranceProgram>();

            insprogramsTable.AutoGenerateColumns = false;
           
            
            InitFormData();
        }

        private void InitFormData()
        {
            ownertype.Items.Clear();
            ownertype.Items.Add("Пациент");
            ownertype.Items.Add("Компания");

            ownertype.SelectedItem = ownertype.Items[0];

            insurercomboBox7.DataSource = Company.GetAllCompanies();
            insurercomboBox7.DisplayMember = "CompanyName";

            List<InsuranceProgram> ip = InsuranceProgram.GetAllInsurancePrograms();

            ins_program.DataSource = ip;
            ins_program.DisplayMember = "InsuranceProgramName";
        }

        private void AddInsurance_Load(object sender, EventArgs e)
        {

        }

        private void ownertype_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ownertype.SelectedIndex == 1)
            {
                ownercomboBox6.Enabled = true;
                ownercomboBox6.DataSource = Company.GetAllCompanies();
                ownercomboBox6.DisplayMember = "CompanyName";
            }
            else { ownercomboBox6.DataSource = null; ownercomboBox6.Enabled = false; }
        }

        private void addinsprogrambutton_Click(object sender, EventArgs e)
        {
            this.insurance.InsurancePrograms.Add((InsuranceProgram)ins_program.SelectedItem);
            insprogramsTable.DataSource = null;
            insprogramsTable.DataSource = this.insurance.InsurancePrograms;
            insprogramsTable.Columns[0].DataPropertyName = "InsuranceProgramName";
            insprogramsTable.Columns[1].DataPropertyName = "Balance";

        }

        private void save_Click(object sender, EventArgs e)
        {
            this.insurance.Balance = float.Parse(balancetextBox12.Text.Trim());
            this.insurance.InsuranceOrderNomer = nomertextBox11.Text;
            this.insurance.EffectiveDate = startdateTimePicker.Value;
            this.insurance.TerminationDate = enddateTimePicker3.Value;
            this.insurance.InsurerCompany = (Company)insurercomboBox7.SelectedItem;
            this.insurance.Owner = (this.ownercomboBox6.SelectedItem == null)?this.patient:(Owner)this.ownercomboBox6.SelectedItem;
            this.patient.Insurances.Add(this.insurance);

            this.Close();

        }
    }
}
